﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class OutStockItemBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public OutStockItemBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string OutStockID, string ProductDetailID)
        {
            if (DAO.T_OUTSTOCK_ITEMs.Where(t => t.OUTSTOCK_ID.ToString() == OutStockID
                                                && t.PRODUCT_DETAIL_ID.ToString() == ProductDetailID).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_OUTSTOCK_ITEM> GetOutStockItemList()
        {
            var list = DAO.T_OUTSTOCK_ITEMs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_OUTSTOCK_ITEM GetOutStockItemByID(string ID)
        {
            return DAO.T_OUTSTOCK_ITEMs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddOutStockItem(T_OUTSTOCK_ITEM OutStockItem)
        {
            try
            {
                OutStockItem.ID = DAO.GetGUID();

                DAO.T_OUTSTOCK_ITEMs.InsertOnSubmit(OutStockItem);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddOutStockItem(T_OUTSTOCK_ITEM OutStockItem, out string OutStockItemGuid)
        {
            try
            {
                OutStockItem.ID = DAO.GetGUID();
                
                DAO.T_OUTSTOCK_ITEMs.InsertOnSubmit(OutStockItem);
                DAO.SubmitChanges();

                OutStockItemGuid = OutStockItem.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                OutStockItemGuid = "";
                return -1;
            }
        }

        public int UpdateOutStockItem(T_OUTSTOCK_ITEM OutStockItem)
        {
            try
            {
                T_OUTSTOCK_ITEM updateEntity = DAO.T_OUTSTOCK_ITEMs.SingleOrDefault(t=>t.ID.ToString()==OutStockItem.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.FLOOR_ID = OutStockItem.FLOOR_ID;
                updateEntity.GROSS_WEIGHT = OutStockItem.GROSS_WEIGHT;
                updateEntity.NET_WEIGHT = OutStockItem.NET_WEIGHT;
                updateEntity.OUTSTOCK_ID = OutStockItem.OUTSTOCK_ID;
                updateEntity.PACKET_QTY = OutStockItem.PACKET_QTY;
                updateEntity.PRODUCT_DETAIL_ID = OutStockItem.PRODUCT_DETAIL_ID;
                updateEntity.REMARK = OutStockItem.REMARK;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteOutStockItem(string ID)
        {
            try
            {
                T_OUTSTOCK_ITEM deleteEntity = DAO.T_OUTSTOCK_ITEMs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_OUTSTOCK_ITEMs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteOutStockItems(List<string> IDs)
        {
            try
            {
                foreach (string ID in IDs)
                {
                    T_OUTSTOCK_ITEM deleteEntity = DAO.T_OUTSTOCK_ITEMs.SingleOrDefault(t => t.ID.ToString() == ID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_OUTSTOCK_ITEMs.DeleteOnSubmit(deleteEntity);
                }

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
